package com.useeinfo.oa.modules.inspection.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.inspection.entity.InspectionData;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("inspectionDataDao")
public class InspectionDataDao implements CrudDao<InspectionData> {
    private final static Logger logger = LoggerFactory.getLogger(InspectionDataDao.class);
    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();
        conditionHash.put("s.deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }
        String name = queryHash.get("name");
        if (!StringUtils.isEmpty(name)) {
            conditionHash.put("(s.name = ?{paramIndex}) ", name);
        }
        String timeStamp = queryHash.get("timeStamp");
        if (!StringUtils.isEmpty(timeStamp)) {
            conditionHash.put("(s.timeStamp = ?{paramIndex}) ", StringConverters.ToLong(timeStamp));
        }
        String searchCode = queryHash.get("searchCode");
        if (!StringUtils.isEmpty(searchCode)) {
            conditionHash.put("(s.name like ?{paramIndex}) ", "%"+searchCode+"%");
        }
        String enterpriseId = queryHash.get("enterpriseId");
        if (!StringUtils.isEmpty(enterpriseId)) {
            conditionHash.put("(s.enterprise.id = ?{paramIndex}) ", StringConverters.ToLong(enterpriseId));
        }
        String inspectionTechnologyId = queryHash.get("inspectionTechnologyId");
        if (!StringUtils.isEmpty(inspectionTechnologyId)) {
            conditionHash.put("(s.inspectionTechnology.id = ?{paramIndex}) ", StringConverters.ToLong(inspectionTechnologyId));
        }

        String beforeTimeStamp = queryHash.get("beforeTimeStamp");
        if (!StringUtils.isEmpty(beforeTimeStamp)) {
            conditionHash.put("(s.timeStamp < ?{paramIndex}) ", StringConverters.ToLong(beforeTimeStamp));
        }

        String noId = queryHash.get("noId");
        if (!StringUtils.isEmpty(noId)) {
            conditionHash.put("(s.inspectionTechnology.id is null and 1=?{paramIndex}) ", 1);
        }

        //PC端维保工艺管理编辑页面专用
        String inspectionTechnologyId2 = queryHash.get("inspectionTechnologyId2");
        String timeStamp2 = queryHash.get("timeStamp2");
        if(!StringUtils.isEmpty(timeStamp2)){
            if(StringUtils.isEmpty(inspectionTechnologyId2)){
                //新增 只查时间戳
                conditionHash.put("(s.timeStamp = ?{paramIndex}) ", StringConverters.ToLong(timeStamp2));
            }else{
                //编辑
                conditionHash.put("(s.timeStamp = ?{paramIndex} or s.inspectionTechnology.id = "+inspectionTechnologyId2+") ", StringConverters.ToLong(timeStamp2));
            }
        }

        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(s) from InspectionData s ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<InspectionData> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select s from InspectionData s ", "order by s.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<InspectionData> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, InspectionData.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public InspectionData findModel(Long id) {
        return entityManager.find(InspectionData.class, id);
    }

    @Override
    public Integer add(InspectionData model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("InspectionDataDaoImpl添加inspectionData成功！");
        return 1;
    }

    @Override
    public Integer update(InspectionData model) {
        model.setUpdateDate(new Date());
        entityManager.merge(model);
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        InspectionData inspectionData = entityManager.find(InspectionData.class, id);
        entityManager.remove(inspectionData);
        return 1;
    }

    @Override
    public Integer deleteLogic(InspectionData model) {
        model.setDeleteStatus(true);
        return 1;
    }
}
